#include "stdafx.h"
#include "StaticProgress.h"


IMPLEMENT_DYNAMIC(CStaticProgress, CStatic)

BEGIN_MESSAGE_MAP(CStaticProgress, CStatic)
    ON_WM_PAINT()
END_MESSAGE_MAP()



CStaticProgress::CStaticProgress()
{
    mn_value = 0;
    mn_max = 1;
    mn_ovr_cap = mn_max_ovr = 0;

    mb_auto_valueshow = true;

    mc_col1 = RGB(72,222,72);
    mc_col2 = RGB(110,250,110);
}

CStaticProgress::~CStaticProgress()
{

}


void CStaticProgress::ShowValues(bool enable /*= true*/)
{
    mb_auto_valueshow = enable;
}


void CStaticProgress::SetValue(int v)
{
    mn_value =v;
    Invalidate();
}


void CStaticProgress::SetCap(int over_cap)
{
    mn_ovr_cap = over_cap;
    Invalidate();
}

void CStaticProgress::SetMax(int cap, int over_cap)
{
    mn_max = cap;
    mn_max_ovr = over_cap;
    Invalidate();
}


void CStaticProgress::SetColors(COLORREF c1, COLORREF c2)
{
    mc_col1 = c1;
    mc_col2 = c2;
    Invalidate();
}



void CStaticProgress::OnPaint()
{
    CPaintDC dc(this);

    CString mytext;
    GetWindowText(mytext);

    CRect rect;
    GetClientRect(rect);

    CFont* f_old = dc.SelectObject(GetFont());


    dc.FillSolidRect(rect,GetSysColor(COLOR_BTNFACE));

    int total_w = rect.Width();
    if (mb_auto_valueshow) total_w = total_w*80/100;
    int v1 = total_w*mn_max/(mn_max+mn_max_ovr);
    int v2 = total_w*mn_ovr_cap/(mn_max+mn_max_ovr);

    dc.FillSolidRect(0,rect.Height()-3,v1,3,mc_col1);
    if (v2>0) dc.FillSolidRect(v1,rect.Height()-3,v2,3,mc_col2);

    int r1 = min(rect.Width(),rect.Width()*mn_value/(mn_max+mn_max_ovr));


    if (r1<v1)
    {
        dc.FillSolidRect(0,0,r1,rect.Height(),mc_col1);
    }
    else
    {
        dc.FillSolidRect(0,0,v1,rect.Height(),mc_col1);
        dc.FillSolidRect(v1,0,min(v2,r1-v1),rect.Height(),mc_col2);
    }


    dc.SetBkMode(TRANSPARENT);

    if (mb_auto_valueshow)
    {
        int dist = mn_max+mn_ovr_cap-mn_value;
        CString txt;
        txt.Format(_T("%i (%i)"),mn_value,dist);

        if (dist<0)
            dc.SetTextColor(RGB(192,0,0));

        dc.DrawText(txt,rect,DT_RIGHT);

        CRect temp;
        dc.DrawText(txt,temp,DT_RIGHT | DT_CALCRECT);
        rect.right -= temp.Width()+2;
    }


    dc.DrawText(mytext,rect,DT_LEFT);



    dc.SelectObject(f_old);
}
